//
//  GHPickerView.swift
//  IntelligentMedical
//
//  Created by apple on 2018/8/30.
//  Copyright © 2018年 guohao. All rights reserved.
//

import UIKit

protocol GHSinglePickerViewDelegate: NSObjectProtocol {
    // 确定选择
    func confirmAction(_ result: String)
}

class GHSinglePickerView: UIView, UIPickerViewDataSource, UIPickerViewDelegate {
    
    weak var delegate: GHSinglePickerViewDelegate?
    
    let multiple: CGFloat = {       // ipad 1.5倍
        return (UI_USER_INTERFACE_IDIOM() == .pad) ? 1.5 : 1.0
    }()
    
    var dataArray = [String]()
    
    
    public init(frame: CGRect, data: [String]) {
        super.init(frame: frame)
        
        self.dataArray = data
        self.backgroundColor = UIColor.init(white: 0.0, alpha: 0.50)
        
        self.addSubview(self.selectBgView)
        self.pickerView.reloadAllComponents()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var selectBgView: UIView = {
        let _selectBgView = UIView.init(frame: CGRect.init(x: 0, y: SCREEN_HEIGHT - (256 * multiple), width: self.frame.width, height: 256 * multiple))
        _selectBgView.backgroundColor = UIColor.init(hexString: "#ededed")
        _selectBgView.addSubview(self.titleView)
        _selectBgView.addSubview(self.pickerView)
        return _selectBgView
    }()
    
    lazy var titleView: UIView = {
        let _titleView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.frame.width, height: 39 * multiple))
        _titleView.backgroundColor = UIColor.white
        _titleView.addSubview(self.titleLabel)
        _titleView.addSubview(self.confirmButton)
        return _titleView
    }()
    
    lazy var titleLabel: UIView = {
        let _titleLabel = UILabel.init()
        _titleLabel.text = "Select the calculated value"
        let size: CGSize = _titleLabel.text!.size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16.0 * multiple)])
        _titleLabel.frame = CGRect.init(origin: CGPoint.init(x: (SCREEN_WIDTH - size.width) / 2.0, y: (39 - size.height) / 2.0), size: size)
        _titleLabel.textColor = UIColor.init(hexString: "#333333")
        _titleLabel.font = UIFont.systemFont(ofSize: 13 * multiple)
        _titleLabel.textAlignment = .center
        return _titleLabel
    }()

    lazy var confirmButton: UIButton = {
        let _confirmButton = UIButton.init(type: .custom)
        _confirmButton.frame = CGRect.init(x: self.frame.width - 75 * multiple, y: 5 * multiple, width: 51 * multiple, height: 30 * multiple)
        _confirmButton.setTitle("sure", for: .normal)
        _confirmButton.setTitleColor(UIColor.white, for: .normal)
        _confirmButton.backgroundColor = UIColor.init(hexString: "#0F1971")
        _confirmButton.titleLabel?.font = UIFont.systemFont(ofSize: 15 * multiple)
        _confirmButton.layer.cornerRadius = 3
        _confirmButton.addTarget(self, action: #selector(confirmSelected), for: .touchUpInside)
        return _confirmButton
    }()
    
    lazy var pickerView: UIPickerView = {
        let _pickerView = UIPickerView.init(frame: CGRect.init(x: 0, y: 40 * multiple, width: self.frame.width, height: 216 * multiple))
        _pickerView.backgroundColor = UIColor.white
        _pickerView.dataSource = self
        _pickerView.delegate = self
        return _pickerView
    }()
    
    
    @objc func confirmSelected() {
        
        let result = self.dataArray[self.pickerView.selectedRow(inComponent: 0)]

        if delegate != nil {
            delegate?.confirmAction(result)
        }
    }
    
    
    // MARK: - UIPickerViewDataSource
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.dataArray.count
    }
    
    
    // MARK: - UIPickerViewDelegate
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        return self.dataArray[row]
    }
    
}
